(function(E){

E.plugin("MenuBold, MenuJustifyLeft", {
	cmd:[],
	arrCmdList:[],
	icon:{},
	config:{},
	lang:{},
	
	init: function(){
		var self=this;
		switch(self.name){
			case 'MenuBold':
				self.icon={
					img:'icon_mce.gif',
					XY:'0 0',
					width:32,
					menu:1
				}
				self.config={
					width:90
				}
				self.lang={
					'zh-cn':{
						title:''
					}
				}
				self.arrCmdList=['Bold','Italic','Underline','SubScript','SuperScript','StrikeThrough'];
			break;
			case 'MenuJustifyLeft':
				self.icon={
					img:'icon_mce.gif',
					XY:'-460px 0',
					width:32,
					menu:1
				}
				self.config={
					width:120
				}
				self.lang={
					'zh-cn':{
						title:''
					}
				}
				self.arrCmdList=['JustifyLeft','JustifyCenter','JustifyRight','JustifyFull','InsertOrderedList','InsertUnorderedList','Outdent','Indent'];
			break;
		}
	},
	
	click:function($btn, dom, eid){
		this.set($btn, dom, eid);
		var self=this, cmd=this.cmd[eid]|| self.arrCmdList[0], Plugin=E.plugin(cmd);
		Plugin['click']($btn, dom, eid);
	},
	menu:function($btn, dom, eid){
		this.set($btn, dom, eid);
		//name 是插件的名称
		var self=this, name=this.name, $P, html='',
		Panel=E.mod("Panel"), option=E.options[eid], Toolbar=E.mod('Toolbar');
		
		html = Toolbar.getMenuHtml(self.arrCmdList, option);
		$P=Panel.show(html, eid, self);
		
		$P.find("table.menu tr").unbind('click').click(function(){
			var pname=$(this).attr('name'), Plugin=E.plugin(pname);
			Plugin['click']($btn, dom, eid);
			
			var img=$(this).find('img');
			
			if(img.length){
				self.cmd[eid] = pname;
				$("#"+eid+"_"+name+" img").css('backgroundPosition', img.css('backgroundPosition'));
			}
			
			Panel.hide($btn);
		});
		
		return 1;
	}
});

E.plugin("Filter", {
	icon:{
		img:'icon_mce.gif',
		width:40,
		menu:1
	},
	config:{
		width:120
	},
	lang:{
		'zh-cn':{
			title:'过滤'
		}
	},
	click:function($btn, dom, eid){
		this.style2font(dom);
		
	},
	menu:function($btn, dom, eid){
		this.set($btn, dom, eid);
		//name 是插件的名称
		var self=this, name=this.name, $P, html='',
		Panel=E.mod("Panel"), option=E.options[eid], Toolbar=E.mod('Toolbar');
		this.font2style(dom);
		
		return 1;
	},
	
	//允许保留的标签、属性和样式
	allowTags: {
		'default':{
			'font':[],
			'span' : ['class','style', '.font-weight', '.font-style', '.text-decoration'],
			'div' : ['class','style','.width','.text-align'],
			'a' : ['class','href', 'target', 'title', 'alt'],
			'img' : ['class', 'src', 'alt', 'title', 'align', '/'],
			'hr' : ['/'],
			'br' : ['/'],
			'p' : ['align','style','.text-align'],
			'table' : ['class','style','width','.width','align'],
			'tbody': [],
			'tr': [],
			'td': ['class','align','colspan','rowspan'],
			'ol': ['class'],
			'ul': ['class'],
			'li': ['class'],
			'sub': ['class'],
			'sup': ['class','name'],
			'blockquote': [],
			'h2': [],
			'h3': [],
			'h4': [],
			'h5': [],
			'h6': [],
			'em': [],
			'strong': ['style','.width'],
			'b': [],
			'i':[],
			'u': [],
			'strike': [],
			'object':['classid','class','id','width','height','codebase'],
			'embed' : ['style','src', 'type', 'loop', 'autostart', 'quality', '.width', '.height','flename','width','height','volume','pluginspage','console','controls','/'],
			'param':['name','value']
		}
	},
	//清理内容当中多余的标签和属性
	clean: function(dom, allowTags){
		allowTags = allowTags || this.allowTags;
		var html='', num1=0, num2=0, num3=0,num_attr=0, num_style=0, num_node=0, attList=[];
		var nodeAttrs = ['onclick','onmouseout','onmouseover','onmousemove','align'
			,'color','font','height','vspace','hspace','id','class','style','left'
			,'right','name','rel','size','title','width','valign','bgcolor','dir'];
		
		var scanNodes = function(el){
			var nodes = el.childNodes;
			for (var i = 0; i < nodes.length; i++){
                var node = nodes[i];
                if (1 == node.nodeType){
					var tagName = node.tagName.toLowerCase();
					if (allowTags[tagName]){
						var attrList = ($.browser.msie)? nodeAttrs : node.attributes;
						//var attrList = node.attributes;
						var styles = node.style;
						if($.browser.msie && styles.cssText){
							var _styles=[], styleList = styles.cssText.replace(/ /g, '').split(';');
							for (var j=0; j<styleList.length; j++){
								_styles.push(styleList[j].split(':')[0]);
							}
							styles = _styles;
						}
						var attr, attrs = allowTags[tagName], attrName;
						
						//过滤属性
						for (var j = 0; j < attrList.length; j++){
							attrName = ($.browser.msie) ? attrList[j] : attrList[j].name.toLowerCase();
							//attrName = attrList[j].name.toLowerCase();
							num1++;
							if ($.inArray(attrName, attrs) == -1){
								node.removeAttribute(attrName);
								num_attr++;
								attList[attrName] = node.getAttribute(attrName);
							}
						}
						
						//过滤样式
						for (var j = 0; j < styles.length; j++){
							attrName = styles[j];
							num2++;
							if ($.inArray('.'+attrName.toLowerCase(), attrs) == -1){
								if($.browser.msie){
									num_style++;
									var re = new RegExp(attrName+": \\w+", 'gi');
									node.style.cssText = node.style.cssText.replace(re, '');
								}else{
									num_style++;
									node.style[attrName] = '';
								}
							}
						}
						
						if (node.hasChildNodes()) {
	                        scanNodes(node);
	                    }
					}else if (node.hasChildNodes()){
						//对于有子元素的不合法标签进行深入遍历，保证删除标签而保留其内容。
						for(var j=0; j<node.childNodes.length; j++){
							scanNodes(node.childNodes[j]);
							node.parentNode.insertBefore(node.childNodes[j].cloneNode(true), node);
						}
						node.parentNode.removeChild(node);
						num_node++;
						num3++;
					}else{
						//没有子元素的不合法标签，直接删除
						node.parentNode.removeChild(node);
						num_node++;
						num3++;
					}
				}
			}
			
		};//function scanNodes
		
		scanNodes(dom.body);
		//'过滤掉'+ num_node +'个节点，'+ num_attr +'个属性，'+ num_style +'个样式。';
		return dom.body.innerHTML;
	},
	
	//清除空标签
	clearEmptyTag: function(dom){		
		/*//貌似多余
		//有时正则不能完全清除空标签
		var bool,els, tags = ['STRONG','SPAN','P'];
		for (j=0;j<tags.length;j++){
			els = dom.getElementsByTagName(tags[j]);
			for(i=0; i<els.length; i++){
				if ($.trim(els[i].innerHTML) == ''){
					els[i].parentNode.removeChild(els[i]);
				}
			}
		}
		*/
		
		els = dom.getElementsByTagName('TABLE');
		var tds, imgs, title;
		for(var i=0; i<els.length; i++){
			bool = true;
			tds = els[i].getElementsByTagName('TD');
			for (j=0;j<tds.length;j++){
				if (tds[j].innerHTML != ''){
					bool = false;
					break;
				}
			}
			if (bool){
				els[i].parentNode.removeChild(els[i]);
			}
		}
	},
	
	/*
	* 将所有的 FONT 标签转为 span 标签，但最多转换1000个
	*/
	font2style: function(dom, eid){
		var self=this, num=0;
		var fontSize={
              "1" : "8",
              "2" : "10",
              "3" : "12",
              "4" : "14",
              "5" : "18",
              "6" : "24",
              "7" : "36"
		}
		//font to style
		
		function change(el){
			var font=$(el), html=font.html(), style=font.attr('style'),
				size=font.attr('size'), color=font.attr('color');
			
			if(style || size || color){
				style = style || '';
				if(size){
					style+= 'font-size:'+fontSize[size]+'px;';
				}
				
				if(color){
					style+= 'color:'+color+';';
				}
				
				html='<span style="'+style+'">'+html+'</span>';
			}
			font.before(html).remove();
		}
		while($('font', dom).length){
			$('font', dom).each(function(){
				change(this);
			});
			if(num++ >= 1000){
				break;//防止死循环
			}
		}
	},
	
	/*
	* 将所有的 span 标签转为 font u b i 标签，但最多转换1000个，将没有任何样式的span标签删除
	*/
	style2font: function(dom, eid){
		var self=this, num=0;
		//style to font
		var fontSize={
              "8px"   : "1",
              "10px"   : "2",
              "12px"   : "3",
              "14px"   : "4",
              "18px"   : "5",
              "24px"   : "6",
              "36px"   : "7"
		}
		
		function change(el){
			var span=$(el), html='', style=span.attr('style');
			
			var color=span.css('color'), bgColor=span.css('backgroundColor'), size=span.css('fontSize'),
				B=span.css('fontWeight'), U=span.css('textDecoration'), I=span.css('fontStyle');
			
			if(!/[^\w]?color\:/i.test(style)) color='';
			if(!/background\-color/i.test(style)) bgColor='';
			if(!/font\-size/i.test(style)) size='';
			
			if(!/font\-weight/i.test(style)) B='';
			if(!/underline/i.test(style)) U='';
			if(!/font\-style/i.test(style)) I='';
			
			if(color || bgColor || size){
				html='<font';
				
				if($.IE){
					if(bgColor){
						html += ' style="background-color:'+bgColor+'"';
					}
					
					if(color){
						html += ' color="'+color+'"';
					}
				}else{
					if(bgColor && color){
						html += ' style="background-color:'+bgColor+';color:'+color+'"';
					}else if(bgColor){
						html += ' style="background-color:'+bgColor+'"';
					}else if(color){
						html += ' style="color:'+color+'"';
					}
				}
					
				if(size){
					html += ' size="'+fontSize[size]+'"';
				}
				
				html += '>'+span.html()+'</font>';
				if(B) html = '<b>'+html+'</b>';
				if(I) html = '<i>'+html+'</i>';
				if(U) html = '<u>'+html+'</u>';
				
				span.before(html).remove();
			}else{
				html = span.html();
				if(B) html = '<b>'+html+'</b>';
				if(I) html = '<i>'+html+'</i>';
				if(U) html = '<u>'+html+'</u>';
				span.before(html).remove();
			}
		}
		
		while($('span', dom).length){
			$('span', dom).each(function(){
				change(this);
			});
			if(num++ >= 1000){
				break;//防止死循环
			}
		}
	}
});

})($.editor)